package com.cn.customer.housekeeping.housekeepingc.ui.fragment;

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.cn.customer.housekeeping.housekeepingc.R;
import com.cn.customer.housekeeping.housekeepingc.bean.BaseResp;
import com.cn.customer.housekeeping.housekeepingc.bean.ServicekindListResp;
import com.cn.customer.housekeeping.housekeepingc.bean.ServicekindResp;
import com.cn.customer.housekeeping.housekeepingc.bean.ShopGridviewResp;
import com.cn.customer.housekeeping.housekeepingc.biz.personcenter.IUserLoginView;
import com.cn.customer.housekeeping.housekeepingc.biz.personcenter.InterfacePresenter;
import com.cn.customer.housekeeping.housekeepingc.bridge.BridgeFactory;
import com.cn.customer.housekeeping.housekeepingc.bridge.Bridges;
import com.cn.customer.housekeeping.housekeepingc.bridge.cache.sharePref.EBSharedPrefManager;
import com.cn.customer.housekeeping.housekeepingc.bridge.cache.sharePref.EBSharedPrefUser;
import com.cn.customer.housekeeping.housekeepingc.constant.Constants;
import com.cn.customer.housekeeping.housekeepingc.constant.URLUtil;
import com.cn.customer.housekeeping.housekeepingc.ui.adapter.PopShopGridViewAdpter;
import com.cn.customer.housekeeping.housekeepingc.ui.base.QuickAdapter;
import com.lfq.pulltorefresh.library.PullToRefreshBase;
import com.lfq.pulltorefresh.library.PullToRefreshRecyclerView;

import java.util.ArrayList;
import java.util.List;


public class ShopOrderListFragment extends Fragment implements IUserLoginView, View.OnClickListener {
    private InterfacePresenter mInterfacePresenter;
    private EBSharedPrefManager manager = BridgeFactory.getBridge(Bridges.SHARED_PREFERENCE);
    private int pageNum = 1;
    private int pageSize = 20;
    private String subtypes, type, cityname;
    private ServicekindResp mServicekindResp;
    private PullToRefreshRecyclerView mPullToRefreshRecyclerView;
    private RecyclerView recyclerView;
    private RecylerViewAdapter adapter;

    public static ShopOrderListFragment newInstance(String type, String subtype, String cityname) {
        Bundle bundle = new Bundle();
        bundle.putString(Constants.typezi, type);
        bundle.putString(Constants.subtypezi, subtype);
        bundle.putString(Constants.cityname, cityname);
        ShopOrderListFragment fragment = new ShopOrderListFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    LinearLayout ll_menu, ll_fenlei;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View contextView = inflater.inflate(R.layout.activity_main_rcyclerviewlist, container, false);
        mInterfacePresenter = new InterfacePresenter();
        mInterfacePresenter.attachView(this);
        mPullToRefreshRecyclerView = (PullToRefreshRecyclerView) contextView.findViewById(R.id.pull_recyclerView);
        ll_menu = (LinearLayout) contextView.findViewById(R.id.ll_menu);
        ll_fenlei = (LinearLayout) contextView.findViewById(R.id.ll_fenlei);
        mPullToRefreshRecyclerView.setScrollingWhileRefreshingEnabled(true);

        mPullToRefreshRecyclerView.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        recyclerView = mPullToRefreshRecyclerView.getRefreshableView();
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        initListeners();
        initData();
        return contextView;
    }


    public void initListeners() {
        ll_fenlei.setOnClickListener(this);

        mPullToRefreshRecyclerView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<RecyclerView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<RecyclerView> refreshView) {
                // 下拉刷新监听

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<RecyclerView> refreshView) {
                // 上拉加载监听
            }
        });
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    private QuickAdapter<ServicekindListResp> mAdapter;

    public void initData() {
        type = getArguments().getString(Constants.typezi);
        cityname = getArguments().getString(Constants.cityname);
        subtypes = getArguments().getString(Constants.subtypezi);
        List<String> list = new ArrayList<>();
        list.add("111");
        list.add("111");
        list.add("111");
        list.add("111");
        list.add("111");
        list.add("111");
        list.add("111");
        list.add("111");
        list.add("111");
        list.add("111");
        list.add("111");
        list.add("111");
        list.add("111");
        list.add("111");
        list.add("111");
        list.add("111");
        list.add("111");
        list.add("111");
        list.add("111");
        list.add("111");
        list.add("111");
        list.add("111");
        list.add("111");
        list.add("111");
        list.add("111");
        list.add("111");


        adapter = new RecylerViewAdapter(list);
        recyclerView.setAdapter(adapter);
        mInterfacePresenter.getServicelist(type, subtypes, pageNum + "", pageSize + "", manager.getKDPreferenceUserInfo().getString(EBSharedPrefUser.lat, ""), manager.getKDPreferenceUserInfo().getString(EBSharedPrefUser.lon, ""), cityname);


    }
    public void popFenlei() {
        View popupView = getActivity().getLayoutInflater().inflate(R.layout.pop_shoporderlist_menu, null);
        GridView mineMainview = (GridView) popupView.findViewById(R.id.mineMainview);

        List<ShopGridviewResp> lists = new ArrayList<ShopGridviewResp>();
        ShopGridviewResp m=new ShopGridviewResp();
        m.setTitle("全部分类1");
        lists.add(m);

        ShopGridviewResp m2=new ShopGridviewResp();
        m2.setTitle("全部分类2");
        lists.add(m2);

        ShopGridviewResp m3=new ShopGridviewResp();
        m3.setTitle("全部分类3");
        lists.add(m3);

        ShopGridviewResp m4=new ShopGridviewResp();
        m4.setTitle("全部分类4");
        lists.add(m4);



        final PopShopGridViewAdpter pictureAdapter = new PopShopGridViewAdpter(getActivity(), lists);
        mineMainview.setAdapter(pictureAdapter);



        mineMainview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View arg1, int select,
                                    long arg3) {
                pictureAdapter.changeStatus(select);//设置选中项
                pictureAdapter.notifyDataSetChanged();//更新列表
            }
        });








        PopupWindow window = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//        window.setAnimationStyle(R.style.popup_window_anim);
        window.setTouchable(true);
        window.setBackgroundDrawable(new BitmapDrawable());
        window.setFocusable(true);
        window.setOutsideTouchable(true);
        window.update();
        window.showAsDropDown(ll_menu, 0, 0);
    }








    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_fenlei://分类
                popFenlei();
                break;
        }
    }

    @Override
    public void onError(BaseResp s) {
        mPullToRefreshRecyclerView.onRefreshComplete();
    }

    @Override
    public void onSuccess(BaseResp s) {
        mPullToRefreshRecyclerView.onRefreshComplete();
        if (s.getUrl().equals(URLUtil.url_get_getservicelist)) {//

        }
    }

    @Override
    public void showLoading() {
    }

    @Override
    public void hideLoading() {
    }


    public class RecylerViewAdapter extends RecyclerView.Adapter<RecylerViewAdapter.ViewHolder> {
        private List<String> list;

        public RecylerViewAdapter(List<String> list) {
            this.list = list;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ViewHolder(getActivity().getLayoutInflater().inflate(android.R.layout.simple_list_item_1, parent, false));
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.textView.setText(list.get(position));
        }

        @Override
        public int getItemCount() {
            return list == null ? 0 : list.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            TextView textView;

            public ViewHolder(View itemView) {
                super(itemView);
                textView = (TextView) itemView.findViewById(android.R.id.text1);
            }
        }
    }

}